package com.ita.acmsystem.merchant.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.codec.binary.Base64;
import org.json.JSONException;
import org.json.JSONObject;

import com.ita.acmsystem.merchant.entity.Merchant;

public class JsonUtil {
	
	private static Base64 base64 = new Base64();
	
	public static String generateJsonFromMerchant(Merchant merchant) {
		JSONObject jsonObject = new JSONObject();
		try {
			jsonObject.put("merchantId", merchant.getMerchantId());
			jsonObject.put("merchantName", merchant.getMerchantName());
			System.out.println(merchant.getMerchantName());
			jsonObject.put("merchantPassword", merchant.getMerchantPassword());
			jsonObject.put("merchantAddress", merchant.getMerchantAddress());
			jsonObject.put("isBlack", merchant.getMerchantBlanklist());
			if (merchant.getMerchantPic() != null) {
				File file = new File(merchant.getMerchantPic());
				BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
				//存储图片字节的可变长字节集合
				ArrayList<Byte> tempBytes = new ArrayList<Byte>();
				byte[] buff = new byte[1024];
				int number = -1;
				while ((number = bis.read(buff, 0, 1024)) != -1) {
					for (int i = 0; i < number; i++) {
						tempBytes.add(buff[i]);
					}
				}
				buff = null;
				// 字节集合转成字节数组
				byte[] merchantPicBytes = listTobytes(tempBytes);
				// Base64加密
				byte[] encryptedBytes = base64.encode(merchantPicBytes);
				String encryptedStr = new String(encryptedBytes);
				jsonObject.put("merchantPic", "data:image/png;base64," + encryptedStr);
				bis.close();
			}
			if (merchant.getMerchantIdcard() != null) {
				File file = new File(merchant.getMerchantIdcard());
				BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
				// 存储图片字节的可变长字节集合
				ArrayList<Byte> tempBytes = new ArrayList<Byte>();
				byte[] buff = new byte[1024];
				int number = -1;
				while ((number = bis.read(buff, 0, 1024)) != -1) {
					for (int i = 0; i < number; i++) {
						tempBytes.add(buff[i]);
					}
				}
				buff = null;
				// 字节集合转成字节数组
				byte[] merchantPicBytes = listTobytes(tempBytes);
				// Base64加密
				byte[] encryptedBytes = base64.encode(merchantPicBytes);
				String encryptedStr = new String(encryptedBytes);
				jsonObject.put("merchantIdcard", "data:image/png;base64," + encryptedStr);
				bis.close();
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsonObject.toString();
	}
	
	private static byte[] listTobytes(List<Byte> list) {
		if (list == null || list.size() < 0)
			return null;

		byte[] bytes = new byte[list.size()];
		int i = 0;
		Iterator<Byte> iterator = list.iterator();
		while (iterator.hasNext()) {
			bytes[i] = iterator.next();
			i++;
		}
		return bytes;
	}
}
